package reporting;

import java.awt.Color;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;

/**
 * Creates a pie chart.
 * 
 * Never actually used in this program.
 * 
 * Future TODO.
 * @author Kevin
 *
 */
@SuppressWarnings("serial")
public class PieChart extends JPanel {

	//private static final long serialVersionUID = 1L;
	
	private static ReportHandler handler;

	public PieChart(String chartTitle, String date, String hid) {
        //super(applicationTitle);
        //PieDataset dataset = createDataset(date);	      				// This will create the dataset 
        //JFreeChart chart = createChart(chartTitle); 					// based on the dataset we create the chart
        //ChartPanel chartPanel = new ChartPanel(chart);     			// we put the chart into a panel
        //chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));        // default size
        //setContentPane(chartPanel);   							    // add it to our application
        
		JPanel chartPanel = createDemoPanel(date, hid, chartTitle);
		chartPanel.setBackground(Color.white);
		this.add(chartPanel);
    }
        
	/**
     * Creates a sample dataset 
     */

    private static PieDataset createDataset(String date, String hid) {
    	handler = new ReportHandler();
        DefaultPieDataset result = new DefaultPieDataset();
        
        String[][] data = handler.getOccupancyRate(date,hid);
        
        for(int i=0;i < data.length;i++) {
        	result.setValue(data[i][0].toString(), Double.parseDouble(data[i][1]));
        }
        
        return result;
    }

    /**
     * Creates a chart
     */

    private static JFreeChart createChart(PieDataset dataset, String title) {
        
        JFreeChart chart = ChartFactory.createPieChart3D(
            title,  				// chart title
            dataset,                // data
            true,                   // include legend
            true,
            false
        );

        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        return chart;
        
    }
    
    public static JPanel createDemoPanel(String chartTitle, String date, String hid) {
        JFreeChart chart = createChart(createDataset(date,hid), chartTitle);
        return new ChartPanel(chart);
    }

    /*
    public static void main(String[] args) {
		PieChart demo = new PieChart("Daily", "Room Occupany Rate", "2012-01-05");
		demo.pack();
		demo.setVisible(true);
    }*/
    
}

